package com.apes.framework.plugin.soe.store.database.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttribute;
import com.apes.framework.plugin.soe.store.database.repository.BusinessAttributeRepository;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.ReflectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.OneToMany;
import javax.persistence.Transient;
import java.beans.PropertyDescriptor;
import java.util.*;

@Service("businessAttributeService")
public class BusinessAttributeService extends DomainService {
    @Autowired
    BusinessAttributeRepository businessAttributeRepository;

    public BusinessAttribute save(SimpleRequest simpleRequest) {
        BusinessAttribute businessAttribute = simpleRequest.getO(BusinessAttribute.class);
        businessAttribute.setId();
        businessAttribute.getBusinessAttributeValues().stream().forEach(bav -> bav.setId());
        return businessAttributeRepository.saveAndFlush(businessAttribute);
    }

    public void saveBusinessAttribute(BusinessAttribute other) {
        BusinessAttribute businessAttribute = businessAttributeRepository.findFirstByBusinessTypeAndCode(other.getBusinessType(), other.getCode());
        if (businessAttribute != null && businessAttribute.equals(other)) return;
        if (businessAttribute == null) {
            businessAttributeRepository.saveAndFlush(other);
            return;
        }
        if (businessAttribute.merge(other)) {
            businessAttributeRepository.saveAndFlush(businessAttribute);
        }
    }

    /*
    * 根据凭证类型查找对应的Bean 属性
    * */
    public List<Map<String, String>> findBeanAttributeByBusinessTypeId(SimpleRequest simpleRequest) {
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        String businessTypeId = simpleRequest.get("businessTypeId");
        Object fieldType = simpleRequest.getMap().get("fieldType");
        try {
            Class bean = JpaUtil.getDomainClass(businessTypeId);
            PropertyDescriptor[] propertyDescriptors = ReflectUtil.getPropertyDescriptors(bean);
            Arrays.stream(propertyDescriptors).filter(descriptor -> {
                if(fieldType == null) return true;
                return descriptor.getPropertyType().getSimpleName().equalsIgnoreCase((String) fieldType);
            }).forEach(
                    (propertyDescriptor) -> {
                        Map map = new HashMap();
                        String name = propertyDescriptor.getName();
                        if("class".equals(name)) return;
                        if (ReflectUtil.existAnnotation(bean, name, OneToMany.class)) return;
                        if (ReflectUtil.existAnnotation(bean, name, Comment.class)){
                            Comment comment = ReflectUtil.getAnnotation(bean, name, Comment.class);
                            map.put("name",comment.value());
                        }else if(ReflectUtil.existAnnotation(bean, name, States.class)) {
                            States states = ReflectUtil.getAnnotation(bean, name, States.class);
                            map.put("name",states.name());
                        }else {
                            if(ReflectUtil.existAnnotation(bean, name, Transient.class)) {
                                map.put("name","扩展字段");
                            }else {
                                map.put("name","未定义请补全");
                            }
                        }
                        map.put("id",name);
                        result.add(map);
                    }
            );
        } catch (Exception e) {
            throw new RuntimeException("未找到对应的Bean类");
        }
        return result;
    }

}
